<?php

namespace yan\backendclient\modules\backendext\models;

use Yii;

use yan\backendclient\LoadConfig;
use yan\backendclient\modules\backendmodule\models\BackendModule;
/**
 * This is the model class for table "{{%backend_extensions}}".
 *
 * @property integer $id
 * @property string $i18n
 * @property integer $status
 * @property string $namespace 扩展表单入口CLASS
 * @property string $target_class 关联入口MODEL class
 * @property string $type
 * @property integer $sort_order
 */
class BackendExtensions extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%backend_extensions}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['i18n'], 'required'],
            [['status', 'sort_order', 'layout'], 'integer'],
            [['i18n'], 'string', 'max' => 64],
            [['namespace', 'target_class'], 'string', 'max' => 255],
            [['type', 'type_key'], 'string', 'max' => 16],
            // [['i18n'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('debug', 'ID'),
            'i18n' => Yii::t('debug', 'I18n'),
            'status' => Yii::t('debug', 'Status'),
            'namespace' => Yii::t('debug', 'Namespace'),
            'target_class' => Yii::t('debug', 'Target Class'),
            'type' => Yii::t('debug', 'Type'),
            'sort_order' => Yii::t('debug', 'Sort Order'),
        ];
    }

    // 加载扩展信息
    public function loadExtensions(){
        $loadConfig = new LoadConfig(Yii::getAlias('@backend'));
        $list = $loadConfig->getModules();
        $data = [];

        // 获取扩展目录信息
        $moduleExtPath = Yii::$app->basePath . DS .'extensions';

        $this->searchDir($moduleExtPath, $data);

        $modulesModels = BackendModule::find()->where(['status'=>1])->asArray()->all();

        $moduleList = [];

        foreach ($modulesModels as $key => $moduleModel) {
            $moduleList[] = $moduleModel['module_id'];
        }
        // 获取模块扩展目录信息
        if(!empty($list)){
            foreach ($list as $key => $val) {
                if( in_array($key, $moduleList) ){
                    $moduleExtPath = Yii::$app->basePath . DS . 'modules' . DS . $key . DS .'extensions';
                    $this->searchDir($moduleExtPath, $data);
                }
            }
        }
        return $data;
    }

    // 查找文件信息
    protected function searchDir($path,&$data,$levevl=0,$path_name='',$flag='', $searchAll=false, $callBack=null){
        // 當二級目錄深度
        if( is_dir($path) && $levevl!=2 && !$searchAll ){
            $dp = dir($path);

            if($levevl==1){
                $path_name = basename($path);
            }

            $levevl++;
            while($file=$dp->read()){

                if($file!='.'&& $file!='..'){

                    $this->searchDir($path.DS.$file,$data,$levevl,$path_name,$flag);
                }
            }
            $dp->close();
        }else if( is_dir($path) && $searchAll && $levevl!==$searchAll ){
            $dp = dir($path);
            $levevl++;

            $path_name .= '/'. basename($path);
                
            while($file=$dp->read()){

                if($file!='.'&& $file!='..'){

                    $this->searchDir($path.DS.$file,$data,$levevl,$path_name,$flag, $searchAll, $callBack);
                }
            }
            $dp->close();
        }
        if(is_file($path)){

            $root_url = dirname(Yii::$app->basePath);
            $class = str_replace([$root_url,'.php'], '' ,$path);
            $path = basename($path, ".php");
            
            $class = str_replace('/', '\\', $class);
            
            try {
                if(strtolower($path) == 'extension' && @class_exists($class)){
                    // $classModel = new $class;
                    // if(is_subclass_of($classModel, 'backend\components\models\ExtensionForm')){
                        $data[] = ['name'=>$path,'class'=>$class];
                    // }$class
                }
            } catch (\Exception $e) {
                
            }
            
        }
    }
}
